package com.lbt.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbt.eduservice.entity.EduChapter;
import com.lbt.eduservice.entity.EduCourse;
import com.lbt.eduservice.entity.EduCourseDescription;
import com.lbt.eduservice.entity.EduVideo;
import com.lbt.eduservice.entity.chapter.ChapterVo;
import com.lbt.eduservice.entity.chapter.VideoVo;
import com.lbt.eduservice.entity.vo.CourseInfoVo;
import com.lbt.eduservice.mapper.EduChapterMapper;
import com.lbt.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbt.eduservice.service.EduCourseDescriptionService;
import com.lbt.eduservice.service.EduVideoService;
import com.lbt.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService videoService;
    //课程列表大纲，根据课程ID 进行查询
    @Autowired
    EduCourseDescriptionService descriptionService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseID(String courseId) {
        //1、 根据课程ID 进行课程里面所有的章节
        QueryWrapper<EduChapter>chapterWrapper=new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);

        //2、 根据课程ID 进行课程里面所有的小节
        QueryWrapper<EduVideo>videoWrapper=new QueryWrapper<>();
        List<EduVideo> videoList = videoService.list(videoWrapper);

        List<ChapterVo>finallist=new ArrayList<ChapterVo>();
        //3、 便利章节list集合进行封装
        for(int i=0;i<chapterList.size();i++){
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            //封装小节
            List<VideoVo>videoVoList=new ArrayList<>();
            //便利所有小阶进行封装
            for(int j=0;j<videoList.size();j++){
                EduVideo eduVideo = videoList.get(j);
                VideoVo videoVo=new VideoVo();
                BeanUtils.copyProperties(eduVideo,videoVo);
                if(eduChapter.getId().equals(eduVideo.getChapterId())){
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
            finallist.add(chapterVo);
        }

        return finallist;
    }
    //章节下面有小节不能删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);

        int count = videoService.count(wrapper);
        if(count>0){ //不删除
           return false;
        }
        int result = baseMapper.deleteById(chapterId);
        return result>0;
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

}
